package com.wyw.base.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.util.DefaultPrettyPrinter;

import com.wyw.base.util.compare.CompareObject;
import com.wyw.base.util.compare.TaskComparator;
import com.wyw.core.workflow.service.support.WorkFlowTask;

public class BaseUtil {
	private final static Log logger = LogFactory.getLog(BaseUtil.class);
	public static String dateFormatYY="yy";
	public static String dateFormatYYMM="yyMM";
	public static String dateFormatYY_MM="yy-MM";
	public static String dateFormatYYYY_MM_DD="yyyy-MM-dd";
	public static String dateFormatYY_MM_DD="yy-MM-dd";
	public static String dateFormatMM_DD="MM-dd";
	 
 
	
	public static List<String> splitAsList(String str, String seperator) {
		List<String> result = new ArrayList<String>();
		if (str == null || str.equals(""))
			return result;
		while (str.indexOf(seperator) >= 0) {
			result.add(str.substring(0, str.indexOf(seperator)));
			str = str.substring(str.indexOf(seperator) + 1);
		}
		result.add(str);

		return result;
	}
	
	public static String getUUID(){
		return   java.util.UUID.randomUUID().toString();
	}
	
	public static boolean isEmpty(String in){
		return (in==null || in.equals(""));
	}
	
	public static boolean isNotEmpty(String in){
		return !(in==null || in.equals(""));
	}
	
	public static void descObject(String info,Object objSrc){
		ObjectMapper objectMapper =new ObjectMapper();
		try{
			logger.info("\r\n"+info+"\r\n"+objectMapper.writer(new DefaultPrettyPrinter()).writeValueAsString(objSrc));
		}catch(Exception e){
			logger.info(e);
		}
	}
	
	public static void descObject(Object objSrc){
		ObjectMapper objectMapper =new ObjectMapper();
		try{
			logger.info("\r\n"+objectMapper.writer(new DefaultPrettyPrinter()).writeValueAsString(objSrc));
		}catch(Exception e){
			logger.info(e);
		}
	}
	
	public static List<Map> sortMapList(List<Map> mapList,String key,String sort){
		CompareObject compareObject;
		List compareList = new ArrayList();
		for(Map map:mapList){
			compareList.add(new CompareObject(key,map));
		}
		Object [] mapkArray = compareList.toArray();
        Arrays.sort(mapkArray, new TaskComparator(sort));
        List<Map> orderList= new ArrayList();
        for(int i=0;i<mapkArray.length;i++){
        	compareObject=(CompareObject)mapkArray[i];
        	orderList.add(compareObject.getComparedMap());
        	//logger.info((compareObject.getKeyValue()));
        }
         return orderList;
	}
	
	public static String toJson(Object objSrc){
		ObjectMapper objectMapper =new ObjectMapper();
		try{
			
		 return objectMapper.writeValueAsString(objSrc);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		 /*JSONObject jSONObject ;
		if(objSrc instanceof Map){
			jSONObject = new JSONObject((Map)objSrc);
			return  jSONObject.toString();
		}else if(objSrc instanceof Collection){
			JSONArray jSONArray=new JSONArray((Collection)objSrc);
			return jSONArray.toString();
		}else{
			jSONObject = new JSONObject(objSrc);
			return  jSONObject.toString();
		}*/
	}
	
	//要么日期，要么数字
	public static String getFormatValue(Object value,String format){
		if(format.equals(dateFormatYYYY_MM_DD) ){
			SimpleDateFormat simpleDateFormat=new java.text.SimpleDateFormat(dateFormatYYYY_MM_DD);
			return simpleDateFormat.format((Date)value);
		}
		return value.toString();
	}
	
	public static void main(String[] args)throws Exception{
		WorkFlowTask workFlowTask = new WorkFlowTask();
		workFlowTask.setBizId("123456");
		workFlowTask.setBizTypeCode("1008");
		List list= new ArrayList();
		list.add(workFlowTask);
		//JSONArray jSONArray=new JSONArray(list);
		//System.out.println(jSONArray.toString());
		 
		Map map = new HashMap();
		map.put("a", "c");
		map.put("d", "e");
		map.put("f", "g");
		map.put("list", list);
		descObject(map);
		
		Object test = new Object();
		
		
		/*System.out.println(org.apache.commons.lang.time.DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
		System.out.println(  );
		org.apache.commons.beanutils.LazyDynaBean g = new  org.apache.commons.beanutils.LazyDynaBean();
		g.set("m", "1");
		g.set("d", "2");
		System.out.println(g.get("m"));*/
		
	}
}
